package com.qianbao.qianbaobusiness.ui.support.businessanalysis.switchshop;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.androidbase.utils.ToastUtils;
import com.baseandroid.config.Global;
import com.baseandroid.event.SwitchAnalysisShopEvent;
import com.baseandroid.mvp.BaseMvpActivity;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.json.MerchantInfo;
import com.qianbao.qianbaobusiness.json.ShopListInfo;
import com.qianbao.qianbaobusiness.ui.cashier.switchshop.SwitchShopAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class AnalysisSwitchShopActivity extends BaseMvpActivity<AnalysisSwithcShopView, AnalysisSwitchShopPresenter> implements SwitchShopAdapter.PickListener {

    @BindView(R.id.tv_title)
    TextView     mTvTitle;
    @BindView(R.id.tv_right_click)
    TextView     mTvRightClick;
    @BindView(R.id.tv_count)
    TextView     mTvCount;
    @BindView(R.id.cb_all)
    CheckBox     mCbAll;
    @BindView(R.id.rv)
    RecyclerView mRv;
    private SwitchShopAdapter  mAdapter;
    private List<MerchantInfo> mList;
    private int                mSelectCount;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_analysis_switch_shop_multi;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void setupView() {
        mTvTitle.setText("切换门店");
        mTvRightClick.setText("完成");
        mTvRightClick.setVisibility(View.VISIBLE);
        mTvRightClick.setTextColor(ContextCompat.getColor(this, R.color.text_red));
        mCbAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    for (MerchantInfo bean : mList) {
                        bean.isSelected = isChecked;
                    }
                } else {
                    if (mSelectCount == mList.size()) {
                        for (MerchantInfo bean : mList) {
                            bean.isSelected = isChecked;
                        }
                    }
                }
                mAdapter.notifyDataSetChanged();
            }
        });
        initRecyclerView();
    }

    private void initRecyclerView() {
        if (mAdapter == null) {
            mAdapter = new SwitchShopAdapter(this, this);
        }
        mRv.setLayoutManager(new LinearLayoutManager(this));
        mRv.setAdapter(mAdapter);
        mList = new ArrayList<>();
        ShopListInfo selectedShopInfo = Global.getSelectedAnalysisShopListInfo();
        mList.addAll(selectedShopInfo.merchantList);
        mAdapter.resetData(mList);
        mTvCount.setText("共" + Global.getShopListInfo().merchantList.size() + "家/已选" + mSelectCount + "家");
    }

    @Override
    protected void setupData(Bundle savedInstanceState) {

    }

    @OnClick({R.id.iv_back_toolbar, R.id.tv_right_click})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back_toolbar:
                finish();
                break;
            case R.id.tv_right_click:
                if (mSelectCount == 0) {
                    ToastUtils.showLongToast("请至少选择一家店");
                } else {
                    ShopListInfo shopListInfo = Global.getShopListInfo();
                    shopListInfo.merchantList.clear();
                    shopListInfo.merchantList.addAll(mList);
                    Global.setSelectedAnalysisShopListInfo(shopListInfo);
                    EventBus.getDefault().post(new SwitchAnalysisShopEvent());
                    finish();
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void pickChange() {
        mSelectCount = 0;
        for (MerchantInfo merchantInfo : mList) {
            if (merchantInfo.isSelected) {
                mSelectCount++;
            }
        }
        mTvCount.setText("共" + mList.size() + "家/已选" + mSelectCount + "家");
        mCbAll.postDelayed(new Runnable() {
            @Override
            public void run() {
                mCbAll.setChecked(mSelectCount == mList.size());
            }
        }, 40);
    }
}
